<template>
  <el-dialog :visible.sync="visible" v-dialogDrag :title="title" :close-on-click-modal="false" class="tl-dialog" width="1200px" @open="open" @close="close">
    <el-form class="tl-form" ref="form" :model="model" :rules="rules" label-width="150px" :disabled="disableEditSubmit">
      <el-row>
        <el-col :span="12">
          <el-form-item label="员工工号" prop="usercode" :rules="rules.validateRequire">
            <tl-ref-user v-model="model.usercode" :initDisplayData.sync="model.usercode" dataType="code" :singleSelect="true" @confirm="confirmUser"></tl-ref-user>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="员工姓名" prop="username">
            <el-input v-model="model.username" disabled></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="项目级别类型" prop="projectleveltype" :rules="rules.validateRequire">
            <el-select v-model="model.projectleveltype" filterable clearable @change="assignProjectLevelValue">
              <el-option v-for="item in codeModel.sdprojectleveltypeList" :key="item.code" :label="item.name" :value="item.code" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="项目级别" prop="arrayprojectlevel" :rules="rules.roleIdList1">
            <el-select v-model="arrayprojectlevel" multiple filterable :disabled="['0','1','2'].indexOf(model.projectleveltype) > -1 ?true:false">
              <el-option v-for="item in codeModel.projectlevelList" :key="item.code" :label="item.name" :value="item.code" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="备注" prop="description">
            <el-input v-model="model.description" type="textarea" :rows="4"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <!-- <el-row>
        <el-col :span="12">
          <el-form-item label="排序码" prop="sortcode">
            <el-input-number v-model="model.sortcode" controls-position="right" :step="1" :min="0" style="width:100%"></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="有效" prop="enabled">
            <el-radio-group v-model="model.enabled">
              <el-radio :label="parseInt(item.code)" v-for="item in codeModel.enabledList" :key="item.code">{{ item.name }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row> -->
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button :type="disableEditSubmit === false ? 'text' : 'primary'" @click="doCancelEdit">取消</el-button>
      <el-button type="primary" @click="doSubmitEdit" :loading="saveLoading" v-if="disableEditSubmit === false">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
/** mixins */
import { mixin_edit } from '@tapui/mixins/mixin_edit'
/** 子组件 */
/** 工具类 */
import { putAction, getAction, postAction, deleteAction } from '@tapui/utils/http'
/** VUE配置 */
export default {
  mixins: [mixin_edit],
  components: {},
  data() {
    var validatePass = (rule, value, callback) => {
        ++this.openNums
        if (this.openNums > 2 && this[rule.field].length === 0) {
          callback(new Error('数据不能为空'))
        }
          callback()
      }
    return {
      openNums: 0,
      moduleTitle: 'PMO标准化人选档案',
      urlPrefix: '/sd/pMOStandardizationRecord/',
      url: {
        // 额外的url
      },
      arraychargeprovinces: [],
      arrayprojectlevel: [],
      addRules: {
        roleIdList: [
          { type: 'array', required: true, trigger: 'change', message: '数据不能为空' }
        ],
        roleIdList1: [
          { validator: validatePass, trigger: 'change', type: 'array' }
        ]
      }
    }
  },
  methods: {
    loaded() {
      if (this.model.chargeprovinces && this.model.chargeprovinces !== '') {
        this.arraychargeprovinces = this.model.chargeprovinces.split(',')
      } else {
        this.arraychargeprovinces = []
      }
      if (this.model.projectlevel && this.model.projectlevel !== '') {
        this.arrayprojectlevel = this.model.projectlevel.split(',')
      } else {
        this.arrayprojectlevel = []
      }
    },
    beforeSubmit() {
      this.$set(this.model, 'chargeprovinces', this.arraychargeprovinces.join(','))
      this.$set(this.model, 'projectlevel', this.arrayprojectlevel.join(','))
      return true
    },
    assignProjectLevelValue() {
      let tempValue = this.model.projectleveltype
      let tempArray = []
      if (tempValue && tempValue !== '') {
        if ('0' === tempValue) {
          tempArray = ['0', '1', '2']
        } else if ('1' === tempValue) {
          tempArray = ['0', '1']
        } else if ('2' === tempValue) {
          tempArray = ['2']
        }
      }
      this.$set(this, 'arrayprojectlevel', tempArray)
    },
    confirmUser(selectionRow) {
      if (selectionRow.length === 0) {
        this.$set(this.model, 'username', '')
        this.$set(this.model, 'usercode', '')
      } else {
        this.$set(this.model, 'username', selectionRow[0].name)
        this.$set(this.model, 'usercode', selectionRow[0].code)
      }
    },
    close() {
      this.openNums = 0
    }
  }
}
</script>

